import { Label, _decorator, Node, Button, EventTouch  } from "cc";
import {oops} from "db://oops-framework/core/Oops";
import {UIID} from "db://assets/script/game/common/config/gameUiConfig";
import {smc} from "db://assets/script/game/common/singletonModuleComp";
import {JoinDeskComp} from "db://assets/script/game/dating/comp/joinDeskComp";
import {ecs} from "db://oops-framework/libs/ecs/ECS";
import {CCComp} from "db://oops-framework/module/common/CCComp";

const { ccclass, property } = _decorator;

/** 滚动消息提示组件,提示完即删除  */
@ccclass('DeskNoKeyboardView')
@ecs.register("DeskNoKeyboardView")
export class DeskNoKeyboardView extends CCComp {
    reset(): void {
    }
    @property({
        type: [Label],
        tooltip: "所有房号 id 格子"
    })
    idList: Label[] = []

    @property({
        type: Node,
        tooltip: "提示窗"
    })
    tipBar: Node

    @property({
        type: Node,
        tooltip: "keyboard"
    })
    keyboard: Node

    @property({
        type: Button,
        tooltip: "关闭按钮"
    })
    closeBtn: Button

    onLoad() {
        // 删除默认的数字
        for (const label of this.idList) {
            label.string = ""
        }
        this.tipBar.active = true
    }

    close() {
        oops.gui.remove(UIID.deskNoKeyboard)
    }

    clickKeyboard(eventTouch: EventTouch) {
        // 获取节点下的 value 节点的 Label 值
        const node = (eventTouch.currentTarget as Node).getChildByName("value")
        const value = node.getComponent(Label).string
        if (value === "10") {
            // 清空
            this.clearInput()
        } else if (value === "12") {
            // 删除一个
            this.delLastInput()
        } else {
            // 输入一个
            this.printNo(value)
        }
    }

    // 显示输入的值
    printNo(value: string) {
        // 隐藏 tip
        if (this.tipBar.active) {
            this.tipBar.active = false
        }
        let i = 0;
        for (; i < this.idList.length; i++) {
            if (this.idList[i].string === "") {
                this.idList[i].string = value
                break
            }
        }
        if (i === this.idList.length - 1) {
            this.activeKeyboardOrNot(false)
            // 刚好6位数，调用接口加入房间
            const comp = new JoinDeskComp()
            comp.deskNo = ""
            this.idList.forEach((value) => {
                comp.deskNo += value.string
            })
            smc.dating.add(comp)
            smc.dating.add(this)
        }
    }

    delLastInput() {
        for (let i = this.idList.length; i > 0; i--) {
            if (this.idList[i-1].string !== "") {
                this.idList[i-1].string = ""
                if (i - 1 === 0) {
                    // 全部删掉了
                    this.tipBar.active = true
                }
                return
            }
        }
    }

    clearInput() {
        for (const label of this.idList) {
            label.string = ""
        }
        this.tipBar.active = true
    }

    // 允许、暂停键盘输入
    activeKeyboardOrNot(isActive: boolean) {
        let btn: Button
        for (const node of this.keyboard.children) {
            btn = node.getComponent(Button)
            btn.interactable = isActive
        }
    }
}